﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WebAPI中实现属性注入精简版.Code;

namespace WebAPI中实现属性注入精简版.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        // 标记需要注入的属性
        [Inject]
        public ILoggerService Logger { get; set; }

        [Inject]
        public IUserService UserService { get; set; }

        // 无参构造函数（模拟无法修改的情况）
        public UserController() { }

        [HttpGet("{id}")]
        public IActionResult Get(int id)
        {
            // 简单验证注入是否成功
            if (Logger == null || UserService == null)
                return StatusCode(500, "依赖注入失败");

            Logger.Log($"获取用户{id}信息");
            var userName = UserService.GetUserName(id);
            return Ok(userName);
        }
    }
}
